﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using GSM.BusinessLayer.DataLayer;
using GSM.BusinessLayer;

namespace CRM_SMS_MARKETING.PL.System_Management
{
    public partial class frmUser : DevExpress.XtraEditors.XtraForm
    {
        UserAccountSql UserAccountsql;
        List<UserAccount> ListUserAccount;
        bool Adding;
        bool Editing;
        string User;
        public frmUser(string _User)
        {
            InitializeComponent();
            txtPassword.Properties.UseSystemPasswordChar = true;
            UserAccountsql = new UserAccountSql();
            User = _User;
        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            Adding = true;
            #region Enabled Button
            btnSave.Enabled = true;
            btnCancel.Enabled = true;
            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            btnNext.Enabled = false;
            btnPrevious.Enabled = false;
            #endregion
            #region Clear textbox

            txtID.Text = ((int)UserAccountsql.GetLastReCordID() + 1).ToString();
            txtUserName.Text = "";
            txtPassword.Text = "";
            txtName.Text = "";
            radioGroupGemder.EditValue = true;
            txtAddress.Text = "";
            txtDistrict.Text = "";
            txtCity.Text = "";
            dateEditBirthday.EditValue = DateTime.Now.AddYears(-12);
            txtHomePhone.Text = "";
            txtCellPhone.Text = "";
            txtEmail.Text = "";
            comboBoxUserType.EditValue = "";

            #endregion
            #region Enabled Control
            btnNew.Enabled = false;
            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            btnNext.Enabled = false;
            btnPrevious.Enabled = false;
            btnSave.Enabled = true;
            btnCancel.Enabled = true;

            txtID.Enabled = false;
            txtUserName.Enabled = true;
            txtName.Enabled = true;
            txtPassword.Enabled = true;
            radioGroupGemder.Enabled = true;
            txtAddress.Enabled = true;
            txtDistrict.Enabled = true;
            txtCity.Enabled = true;
            dateEditBirthday.Enabled = true;
            txtHomePhone.Enabled = true;
            txtCellPhone.Enabled = true;
            txtEmail.Enabled = true;
            comboBoxUserType.Enabled = true;
            #endregion
            #region Set Read Only to TextBox
            txtID.Enabled = false;
            txtUserName.Properties.ReadOnly = false;
            txtPassword.Properties.ReadOnly = false;
            txtName.Properties.ReadOnly = false;
            radioGroupGemder.Properties.ReadOnly = false;
            txtAddress.Properties.ReadOnly = false;
            txtDistrict.Properties.ReadOnly = false;
            txtCity.Properties.ReadOnly = false;
            dateEditBirthday.Properties.ReadOnly = false;
            txtHomePhone.Properties.ReadOnly = false;
            txtCellPhone.Properties.ReadOnly = false;
            txtEmail.Properties.ReadOnly = false;
            comboBoxUserType.Properties.ReadOnly = false;
            #endregion
        }
        private void btnNext_Click(object sender, EventArgs e)
        {
            gridViewUser.MoveNext();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            gridViewUser.MovePrev();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (gridViewUser.RowCount != 0)
            {
                try
                {
                    UserAccountKeys Customerkeys = new UserAccountKeys((int)(gridViewUser.GetFocusedRowCellValue(colID)));

                    GSM.BusinessLayer.UserAccount userAccount = new UserAccount();
                    userAccount = UserAccountsql.SelectByPrimaryKey(Customerkeys);
                    MappingField(userAccount);
                    #region Enable Control
                    btnCancel.Enabled = false;
                    btnSave.Enabled = false;
                    btnNew.Enabled = true;
                    btnDelete.Enabled = true;
                    btnNext.Enabled = true;
                    btnPrevious.Enabled = true;
                    btnEdit.Enabled = true;
                    txtID.Enabled = true;
                    #endregion
                    #region Set Read Only to Textbox
                    txtID.Enabled = true;
                    txtID.Properties.ReadOnly = true;
                    txtUserName.Properties.ReadOnly = true;
                    txtPassword.Properties.ReadOnly = true;
                    txtName.Properties.ReadOnly = true;
                    radioGroupGemder.Properties.ReadOnly = true;
                    txtAddress.Properties.ReadOnly = true;
                    txtDistrict.Properties.ReadOnly = true;
                    txtCity.Properties.ReadOnly = true;
                    dateEditBirthday.Properties.ReadOnly = true;
                    txtHomePhone.Properties.ReadOnly = true;
                    txtCellPhone.Properties.ReadOnly = true;
                    txtEmail.Properties.ReadOnly = true;
                    comboBoxUserType.Properties.ReadOnly = true;
                    #endregion
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }


        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (gridViewUser.RowCount != 0)
            {
                Editing = true;
                #region Enabled Control
                btnNew.Enabled = false;
                btnDelete.Enabled = false;
                btnNext.Enabled = false;
                btnPrevious.Enabled = false;
                btnSave.Enabled = true;
                btnCancel.Enabled = true;
                #endregion
                #region Set Read Only to TextBox
                txtID.Enabled = false;
                txtUserName.Properties.ReadOnly = false;
                txtPassword.Properties.ReadOnly = false;
                txtName.Properties.ReadOnly = false;
                radioGroupGemder.Properties.ReadOnly = false;
                txtAddress.Properties.ReadOnly = false;
                txtDistrict.Properties.ReadOnly = false;
                txtCity.Properties.ReadOnly = false;
                dateEditBirthday.Properties.ReadOnly = false;
                txtHomePhone.Properties.ReadOnly = false;
                txtCellPhone.Properties.ReadOnly = false;
                txtEmail.Properties.ReadOnly = false;
                comboBoxUserType.Properties.ReadOnly = false;
                #endregion
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gridViewUser.RowCount != 0)
            {
                if (MessageBox.Show("Are you sure to delete?", "Confirm Delete", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
                {
                    try
                    {
                        GSM.BusinessLayer.UserAccount userAccount = new UserAccount();
                        #region Mapping field
                        userAccount.UserID = Int32.Parse(txtID.Text);
                        userAccount.UserName = txtUserName.Text;
                        userAccount.Password = txtPassword.Text;
                        userAccount.Name = txtName.Text;
                        userAccount.Gender = (bool)radioGroupGemder.EditValue;
                        userAccount.Address = txtAddress.Text;
                        userAccount.District = txtDistrict.Text;
                        userAccount.City = txtCity.Text;
                        userAccount.Birthday = (DateTime)dateEditBirthday.EditValue;
                        userAccount.HomePhone = txtHomePhone.Text = "";
                        userAccount.CellPhone = txtCellPhone.Text;
                        userAccount.Email = txtEmail.Text;
                        userAccount.UserType = (string)comboBoxUserType.EditValue;
                        userAccount.Deleted = true;
                        #endregion
                        bool check = true;
                        try
                        {
                            UserAccountsql.Update(userAccount);
                        }
                        catch (Exception ex)
                        {
                            check = false;
                            MessageBox.Show(ex.Message);
                        }
                        if (check == true)
                        {

                            MessageBox.Show("Delete Customer Successfully!");
                            gridViewUser.MovePrev();
                            refreshGridUser();
                            //Turn off mode Editing
                            #region  Enabled Control
                            Editing = false;
                            btnSave.Enabled = false;
                            btnCancel.Enabled = false;
                            btnNew.Enabled = true;
                            btnDelete.Enabled = true;
                            btnEdit.Enabled = true;
                            btnNext.Enabled = true;
                            btnPrevious.Enabled = true;
                            #endregion
                        }
                        else
                        {
                            MessageBox.Show("Delete Customer Fail!");
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            bool check = dxValidationProvider1.Validate();
            if (check)
            {
                if (Adding == true)
                {
                    bool checkadd = true;
                    try
                    {
                        GSM.BusinessLayer.UserAccount userAccount = new UserAccount();

                        #region Mapping field
                        userAccount.UserName = txtUserName.Text;
                        userAccount.Password = txtPassword.Text;
                        userAccount.Name = txtName.Text;
                        userAccount.Gender = (bool)radioGroupGemder.EditValue;
                        userAccount.Address = txtAddress.Text;
                        userAccount.District = txtDistrict.Text;
                        userAccount.City = txtCity.Text;
                        userAccount.Birthday = (DateTime)dateEditBirthday.EditValue;
                        userAccount.HomePhone = txtHomePhone.Text = "";
                        userAccount.CellPhone = txtCellPhone.Text;
                        userAccount.Email = txtEmail.Text;
                        userAccount.UserType = (string)comboBoxUserType.EditValue;
                        userAccount.Deleted = false;
                        #endregion

                        UserAccountsql.Insert(userAccount);
                    }
                    catch (Exception ex)
                    {
                        checkadd = false;
                        MessageBox.Show(ex.Message);
                    }
                    if (checkadd == true)
                    {

                        MessageBox.Show("Add Customer Successfully!");
                        refreshGridUser();
                        //Turn off mode Adding
                        #region  Enabled Control
                        Adding = false;
                        btnSave.Enabled = false;
                        btnCancel.Enabled = false;
                        btnNew.Enabled = true;
                        btnDelete.Enabled = true;
                        btnEdit.Enabled = true;
                        btnNext.Enabled = true;
                        btnPrevious.Enabled = true;
                        #endregion
                    }
                    else
                    {
                        MessageBox.Show("Add Customer Fail!");
                    }

                }
                if (Editing == true)
                {
                    bool checkedit = true;
                    try
                    {
                        GSM.BusinessLayer.UserAccount userAccount = new UserAccount();
                        #region Mapping field
                        userAccount.UserName = txtUserName.Text;
                        userAccount.Password = txtPassword.Text;
                        userAccount.Name = txtName.Text;
                        userAccount.Gender = (bool)radioGroupGemder.EditValue;
                        userAccount.Address = txtAddress.Text;
                        userAccount.District = txtDistrict.Text;
                        userAccount.City = txtCity.Text;
                        userAccount.Birthday = (DateTime)dateEditBirthday.EditValue;
                        userAccount.HomePhone = txtHomePhone.Text = "";
                        userAccount.CellPhone = txtCellPhone.Text;
                        userAccount.Email = txtEmail.Text;
                        userAccount.UserType = (string)comboBoxUserType.EditValue;
                        #endregion
                        UserAccountsql.Update(userAccount);

                    }
                    catch (Exception ex)
                    {
                        checkedit = false;
                        MessageBox.Show(ex.Message);
                    }
                    if (checkedit == true)
                    {
                        MessageBox.Show("Edit Customer Successfully!");
                        int i = gridViewUser.GetFocusedDataSourceRowIndex();
                        refreshGridUser();
                        gridViewUser.MoveBy(i);
                        //Turn off mode Editing
                        #region  Enabled Control
                        Editing = false;
                        btnSave.Enabled = false;
                        btnCancel.Enabled = false;
                        btnNew.Enabled = true;
                        btnDelete.Enabled = true;
                        btnEdit.Enabled = true;
                        btnNext.Enabled = true;
                        btnPrevious.Enabled = true;
                        #endregion
               
                    }
                    else
                    {
                        MessageBox.Show("Edit Customer Fail!");
                    }
                }
            }
            else
            {
                MessageBox.Show("Some field are not allowed null or blank value");
            }


        }

        private void refreshGridUser()
        {
            try
            {
                UserAccountsql = new UserAccountSql();
                ListUserAccount = UserAccountsql.SelectByField("Deleted", 0);
                gridControlUser.DataSource = ListUserAccount;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void checkEditShowpassword_CheckedChanged(object sender, EventArgs e)
        {
            if (checkEditShowpassword.Checked == true)
            {
                txtPassword.Properties.UseSystemPasswordChar = false;
            }
            else
            {
                txtPassword.Properties.UseSystemPasswordChar = true;
            }
        }

        private void gridControlUser_Load(object sender, EventArgs e)
        {
            refreshGridUser();
           
        }

        private void MappingField(GSM.BusinessLayer.UserAccount UserAccount)
        {
            #region Maping with textbox
            txtID.Text = UserAccount.UserID.ToString();
            txtUserName.Text = UserAccount.UserName.ToString();
            txtName.Text = UserAccount.Name.ToString();
            txtPassword.Text = UserAccount.Password.ToString();
            radioGroupGemder.EditValue = (bool)UserAccount.Gender;
            txtAddress.Text = UserAccount.Address.ToString();
            txtDistrict.Text = UserAccount.District.ToString();
            txtCity.Text = UserAccount.City.ToString();
            dateEditBirthday.EditValue = UserAccount.Birthday;
            txtHomePhone.Text = UserAccount.HomePhone.ToString();
            txtCellPhone.Text = UserAccount.CellPhone.ToString();
            txtEmail.Text = UserAccount.Email.ToString();
            comboBoxUserType.EditValue = UserAccount.UserType.ToString();
            #endregion
        }

        private void gridViewUser_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                UserAccountKeys CustomerKey = new UserAccountKeys((int)(gridViewUser.GetRowCellValue(e.RowHandle, colID)));
                GSM.BusinessLayer.UserAccount userAccount = new GSM.BusinessLayer.UserAccount();
                userAccount = UserAccountsql.SelectByPrimaryKey(CustomerKey);
                MappingField(userAccount);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gridViewUser_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            gridViewUser_RowClick(sender, e);
        }

        private void gridViewUser_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (ListUserAccount.Count == 0)
            {
                #region Clear textbox

                txtID.Text = "";
                txtUserName.Text = " ";
                txtPassword.Text = "";
                txtName.Text = " ";
                radioGroupGemder.EditValue = true;
                txtAddress.Text = "";
                txtDistrict.Text = "";
                txtCity.Text = "";
                dateEditBirthday.EditValue = null;
                txtHomePhone.Text = "";
                txtCellPhone.Text = "";
                txtEmail.Text = "";
                comboBoxUserType.Text = " ";

                #endregion
            }
            else
            {
                try
                {
                    UserAccountKeys CustomerKey = new UserAccountKeys((int)(gridViewUser.GetFocusedRowCellValue(colID)));
                    GSM.BusinessLayer.UserAccount userAccount = new GSM.BusinessLayer.UserAccount();
                    userAccount = UserAccountsql.SelectByPrimaryKey(CustomerKey);
                    MappingField(userAccount);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
  
        }

        private void comboBoxSearch_EditValueChanged(object sender, EventArgs e)
        {
            if (comboBoxSearch.EditValue.ToString() == "All")
            {
                refreshGridUser();
            }
            else
            gridControlUser.DataSource= UserAccountsql.SelectByField("UserType",comboBoxSearch.EditValue.ToString());
        }

        private void frmUser_Load(object sender, EventArgs e)
        {
            labelUser.Text = User;
        }
    }
}